package com.kgc.web;

import com.kgc.dao.*;
import com.kgc.entity.*;
import com.kgc.utils.PageUtils;
import com.kgc.vo.R;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "userMgeServlet", value = "/userMgeServlet")
public class userMgeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserDao userDao = new UserDao();
        UserCarDao userCarDao = new UserCarDao();
        UserRoomDao userRoomDao = new UserRoomDao();
        PrintWriter writer = response.getWriter();
        String opt = request.getParameter("opt");
        System.out.println("opt = " + opt);
        if ("cy6".equals(opt)) {
            String page = request.getParameter("page");
            String limit = request.getParameter("limit");
            String username = request.getParameter("username");
            PageUtils pageUtils = new PageUtils();
            if (StringUtils.isNotEmpty(page)) {
                pageUtils.setCurrPageNo(Integer.parseInt(page));
            }
            if (StringUtils.isNotEmpty(limit)) {
                pageUtils.setPageSize(Integer.parseInt(limit));
            }
            int total = userDao.totalUser(username);
            pageUtils.init(total);
            List<User> userList = userDao.selectPage(pageUtils, username);
            // 加载用户车位和租房信息
            userList.forEach(u -> {
                // 1.查询当前用户对应的车位
                List<String> cnames = userCarDao.selectUserByUserId(u.getId());
                if (cnames != null && cnames.size() > 0) {
                    u.setCardName(cnames.stream().collect(Collectors.joining(",")));
                } else {
                    u.setCardName("暂无");
                }
                // 1.查询当前用户对应的房屋
                List<String> rnames = userRoomDao.selectRoomByUserId(u.getId());
                if (rnames != null && rnames.size() > 0) {
                    u.setRoomName(rnames.stream().collect(Collectors.joining(",")));
                } else {
                    u.setRoomName("暂无");
                }
            });
            writer.write(R.success("用户列表加载成功..", total, userList).toJSON());
        } else if ("cy6delete".equals(opt)) {
            String id = request.getParameter("id");
            int count = userDao.deleteUser(id);
            if (count > 0) {
                writer.write(R.success("删除成功!").toJSON());
            } else {
                writer.write(R.error("删除失败,数据正在使用中...").toJSON());
            }
        } else if ("cy6edit".equals(opt)) {
            String id = request.getParameter("id");
            String username = request.getParameter("username");
            String phone = request.getParameter("phone");
            String sex = request.getParameter("sex");
            int count = userDao.updateUser(new User(Integer.parseInt(id), username, phone, Integer.parseInt(sex)));
            if (count > 0) {
                writer.write(R.success("修改成功!").toJSON());
            } else {
                writer.write(R.error("修改失败...").toJSON());
            }
        }else if ("cy6stopCar".equals(opt)){
            String id = request.getParameter("id");
            String car = request.getParameter("car");
            int count = userCarDao.stopCar(id, car);
            if (count > 0) {
                writer.write(R.success("停用成功!").toJSON());
            } else {
                writer.write(R.error("停用失败...").toJSON());
            }

        }else if ("cy6stopRoom".equals(opt)){
            String id = request.getParameter("id");
            String room = request.getParameter("room");
            int count = userRoomDao.stopRoom(id, room);
            if (count > 0) {
                writer.write(R.success("停用成功!").toJSON());
            } else {
                writer.write(R.error("停用失败...").toJSON());
            }
        }else if ("cy6danyuan".equals(opt)) {
            List<Danyuan> danyuanList = userRoomDao.selectDanyuanRoom();
            writer.write(R.success("单元列表加载成功..", danyuanList).toJSON());
        }else if ("cy6danyuanRoom".equals(opt)) {
            int did = Integer.parseInt(request.getParameter("did"));
            List<Room> roomList = userRoomDao.selectRoomByDanYuan(did);
            writer.write(R.success("房屋列表加载成功..", roomList).toJSON());
        }else if ("cy6allotRoom".equals(opt)) { // 分配房屋
            System.out.println("123");
            int Id = Integer.parseInt(request.getParameter("id"));
            System.out.println("Id = " + Id);
            int roomId = Integer.parseInt(request.getParameter("roomId"));
            System.out.println("roomId = " + roomId);
            // 修改房屋状态
            int count = userRoomDao.updateRoomStatus(roomId);
            // 添加用户房屋对应信息
            count += userRoomDao.addUserRoom(new UserRoom(Id, roomId));
            if (count > 0) {
                writer.write(R.success("分配成功!").toJSON());
            } else {
                writer.write(R.error("分配失败...").toJSON());
            }
        }else  if ("cy6type".equals(opt)) {  // 查询空闲车位
            int type = Integer.parseInt(request.getParameter("type"));
            List<Car> carList = userCarDao.selectCar(type);
            writer.write(R.success("车位列表加载成功..", carList).toJSON());
        }else if ("cy6allotCar".equals(opt)) { // 分配车位
            int userId = Integer.parseInt(request.getParameter("id"));
            int carId = Integer.parseInt(request.getParameter("carId"));
            // 修改车位状态
            int count = userCarDao.updateCarStatus(carId);
            // 添加用户车位对应信息
            count += userCarDao.addUserCar(new UserCar(userId, carId));
            if (count > 0) {
                writer.write(R.success("分配成功!").toJSON());
            } else {
                writer.write(R.error("分配失败...").toJSON());
            }
        }else if("cy6payment".equals(opt)){
            List<Payment> payments = new paymentDao().selectAll();
            writer.write(R.success("缴费类型列表加载成功..", payments).toJSON());
        }else  if("cy6costMge".equals(opt)){
            String id = request.getParameter("id");
            String type = request.getParameter("type");
            String money = request.getParameter("money");
            Integer count = new CostDao().setPay(id, type, money);
            if (count > 0) {
                writer.write(R.success("设置成功!").toJSON());
            } else {
                writer.write(R.error("设置失败...").toJSON());
            }
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);


    }
}
